<!-- 用户_角色表单弹窗 -->
<form id="sysUser-form" lay-filter="sysUser-form" class="layui-form model-form">
    <input name="user_id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-block">
            <select name="role_id" xm-select="role_id" lay-verify="required">
            </select>
        </div>
    </div>
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="role-submit" lay-submit>保存</button>
    </div>
</form>
<script>
    layui.use(['layer', 'admin', 'form', 'formSelects'], function () {
        var layer = layui.layer;
        var admin = layui.admin;
        var form = layui.form;
        var formSelects = layui.formSelects;
        var config = layui.config;

        form.render('radio');

        // 获取所有角色
        layer.load(2);
        admin.req('api-sys/api/role/list', {}, function (data) {
            layer.closeAll('loading');
            if (0 == data.code) {
                // 渲染多选下拉框
                var roleSelectData = new Array();
                for (var i = 0; i < data.data.length; i++) {
                    roleSelectData.push({name: data.data[i].roleName, value: data.data[i].role_id});
                }
                formSelects.data('role_id', 'local', {arr: roleSelectData});
                // 回显user数据
                var sysUser = admin.getTempData('t_user');
                if (sysUser) {
                    form.val('sysUser-form', sysUser);
                    $('#sysUser-form').attr('method', 'POST');
                }
                admin.req('api-sys/api/role/getRoleByUser?user_id='+sysUser.user_id, {}, function (data) {
                    var v = []
                    data.data.forEach(function(i,x){
                        v[x] = i.role_id;
                    });
                    formSelects.value('role_id',v);
                })
            } else {
                layer.msg('获取角色失败', {icon: 2, time: 500});
            }
        }, 'GET');
        form.on('submit(role-submit)', function (data) {
            layer.load(2);
            console.log(data.field)
            var roles = data.field.role_id.split(",");
            var params = [];
            roles.forEach(function(item,index){
                params[index] = {user_id: data.field.user_id, role_id : item};
            })
            if (data.field.user_id){
                admin.req('api-sys/api/role/updateRoles?access_token=' + config.getToken().access_token, JSON.stringify(params), function (data) {
                    layer.closeAll('loading');
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        admin.finishPopupCenter();
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'PATCH');
            }
            return false;
        });
    });
</script>
